package com.fengjue.product.controller;

import com.fengjue.product.entity.Product;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RefreshScope
public class ProductController {

    @Value("${server.port}")
    private int port;

    /**
     * 展示商品信息
     * @return
     */
    @GetMapping("/product/showMsg")
    public String showMsg(){
        System.out.println("进入商品服务，展示商品,当前端口"+port);
        return "进入商品服务，展示商品，当前服务端口:"+port;
    }

    /**
     * 查询单个商品信息
     * @param productId
     * @return
     */
    @GetMapping("/product/findOne")
    public Map<String,Object> findOne(String productId){

        System.out.println("productId = " + productId);
        Map<String, Object> map = new HashMap<>();
        map.put("status",true);
        map.put("msg","根据id查询商品成功"+port);
        map.put("productId",productId);
        return map;
    }
    @PostMapping("/product/update")
    public Map<String,Object> update(@RequestBody Product product){

        System.out.println("product = " + product);
        Map<String, Object> map = new HashMap<>();
        map.put("status",true);
        map.put("msg","更新商品成功"+port);
        map.put("productId",product);
        return map;
    }

    @GetMapping("/product/break")
    @HystrixCommand(fallbackMethod = "hystrixFailBack")
    public String hystrix(Integer id){
        if (id<0){
            throw new RuntimeException("非法参数，id必须大于0");
        }
        return "id="+id;
    }
    // 熔断的方法
    public String hystrixFailBack(Integer id){
        return "当前传入的id：="+id+"不是有效参数，快速返回";

    }


}
